package com.ln.point.action;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ln.adoms.action.BaseAction;
import com.ln.core.orm.PageData;
import com.ln.core.utils.WebUtils;
import com.ln.point.entity.PointRule;
import com.ln.point.service.PointRuleService;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/point/rule")
public class PointRuleAction extends BaseAction {
    @Resource
    private PointRuleService ruleService;

    @RequestMapping("/list")
    public ModelMap list(HttpServletRequest request) {
        PageData<PointRule> pageData = new PageData<PointRule>();

        String params = request.getParameter("filter_LIKES_iCode_OR_point");

        // 给pageData设置参数
        WebUtils.setPageDataParameter(request, pageData);

        if (StringUtils.isNotBlank(params)) {
            String hql = "from PointRule where lower(iCode) like lower(?) or point = ?";
            Double point = 0.0;
            try {
                point = Double.parseDouble(params);
            } catch (Exception e) {
            }
            pageData = ruleService.find(hql, pageData, "%" + params + "%", point);
        } else {
            pageData = ruleService.find(pageData);
        }

        return new ModelMap(pageData);
    }

    @RequestMapping(value = "/new")
    public String addForm(Model model) {
        return "/point/rule/input";
    }

    @RequestMapping(value = "/edit/{id}")
    public String editForm(@PathVariable("id") Integer id, Model model) {
        PointRule rule = ruleService.find(id);
        model.addAttribute(rule);
        return "/point/rule/input";
    }

    @RequestMapping(value = "/ruleDetail/{iCode}")
    public String ruleDetail(@PathVariable("iCode") String iCode, Model model) {
        PointRule rule = ruleService.find("iCode", iCode);
        if (rule != null) {
            model.addAttribute(rule);
        }
        return "/point/rule/detail";
    }

    @RequestMapping(value = "/repeat", method = RequestMethod.POST)
    @ResponseBody
    public boolean repeat(Integer ruleId, String iCode, Model model) {

        if (ruleId == null || ruleId == 0) {
            PointRule rule = ruleService.find("iCode", iCode);
            if (rule != null) {
                return false;
            }
        }
        return true;
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(PointRule rule) {
        if (rule.isNew()) {
            ruleService.save(rule);
        } else {
            ruleService.update(rule);
        }

        return "redirect:/point/rule/list";// 重定向
    }

    @RequestMapping(value = "/delete/{id}")
    public String delete(@PathVariable("id") Integer id) {
        ruleService.delete(id);
        return "redirect:/point/rule/list";// 重定向
    }

}
